const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import Utils = require("../../utils/Utils");
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
//止住眼泪的秘诀 关卡3
@ccclass
export default class DeputyDontCryView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;
    @property(sp.Skeleton)
    spineBoy:sp.Skeleton = null;
    @property(sp.Skeleton)
    spineGirl:sp.Skeleton = null;
    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    _done = false;
    _dic = 1;
    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.spineBoy.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.spineBoy.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE3");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES3");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    start(){
        Utils.audioManager.playSound("huazhuang/deputyGame/step3_ku");
    }
    onTouchStart(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.spineBoy.node.parent.convertToNodeSpaceAR(worldPoint);
        if(localPoint.y > this.spineBoy.node.position.y && (this.spineBoy.node.angle > -90 && this.spineBoy.node.angle < 90)){
            this._dic = 1;
        }else{
            this._dic = 2;
        }
    };  

    onTouchMove(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.spineBoy.node.parent.convertToNodeSpaceAR(worldPoint);
        let boyPos = this.spineBoy.node.position;
        let angle = Utils.utils.getAngle(localPoint,boyPos);
        if(this._dic == 1){
            this.spineBoy.node.angle = angle + 90;
        }else{
            this.spineBoy.node.angle = angle - 90;
        }
        if ((this.spineBoy.node.angle > 170 && this.spineBoy.node.angle < 190) ||
            (this.spineBoy.node.angle > -190 && this.spineBoy.node.angle < -170)) {
            this._done = true;
            this.spineBoy.node.angle = 0;
            Utils.audioManager.playSound("huazhuang/deputyGame/step3_nvxiao");
            this.spineBoy.setAnimation(0, "2", true);
            this.spineGirl.setAnimation(0, "2", true);
            //@ts-ignore
            this.scheduleOnce(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            },2);
            
        }
    };
    
    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };
    
    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS3"));
    };

    
    
}
